﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.BLL.Constants;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.ServiceStation
{
    public partial class Message : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
            }
            else
            {
                InitData();
            }
        }

        private void InitData()
        {
            #region
            var employeeList = EmployeesManager.GetEmployeesByJobTitle(CodeDictionaryConstants.DeliveryGuys);
            sel_Employees.Items.Add(new ListItem("-请选择-", "-1"));
            employeeList.ForEach(
                item =>
                {
                    if (item.Status != 255)
                    {
                        var listItem = new ListItem(item.Name, item.Id.ToString());
                        sel_Employees.Items.Add(listItem);
                    }
                }
             );
            #endregion
            var orderInfo = OrderInfoManager.GetByOrderInfoId(int.Parse(Request["id"]));
            var orderItemList = OrderInfoManager.GetOrderItemListBy(orderInfo.OrderCode);

            if (orderInfo.OrderCode > 0 && orderItemList.Count > 0)
            {
                var listItem = sel_Employees.Items.FindByValue(orderInfo.DeliveryMan.ToString());
                if (listItem != null)
                {
                    listItem.Selected = true;
                }

                #region
                Dictionary<int, string> restaurantList = new Dictionary<int, string>();
                orderItemList.ForEach(
                   item =>
                   {
                       if (!restaurantList.ContainsKey(item.RestaurantId))
                       {
                           restaurantList.Add(item.RestaurantId, item.RestaurantName);
                       }
                   }
                );
                var text = orderInfo.OrderCode.ToString() + "/";
                foreach (var rest in restaurantList)
                {
                    text += rest.Value + "-";
                }
                text += "/" + orderInfo.Address + "/";
                text += orderInfo.Name + (orderInfo.Gender == 0 ? "先生" : "女士") + "/";
                text += orderInfo.MobileTelephone + "/" + orderInfo.LatestTime.ToString("yyyy-MM-dd HH:m;s");
                this.txt_ClientInfo.Value = text;
                #endregion

                text = orderInfo.OrderCode.ToString() + "/";
                foreach (var rest in restaurantList)
                {
                    text += "付" + rest.Value + GetRestaurantAmount(rest.Key, orderItemList) + "元 ";
                }
                text += "/共" + orderItemList.Count.ToString() + "数量/";
                text += "收客户" + (orderInfo.Freight + orderInfo.Total).ToString("F2") + "元";
                this.txt_OrderInfo.Value = text;
            }
        }

        private string GetRestaurantAmount(int restaurantId, List<OrderItemModel> orderItem)
        {
            decimal result = 0;
            foreach (var item in orderItem)
            {
                if (item.RestaurantId == restaurantId)
                    result += item.Number * item.Price;
            }
            return result.ToString("F2");
        }
    }
}
